import axios from 'axios'

axios.defaults.baseURL = "http://106.55.42.58/stat/overflow/api/v1"
// axios.defaults.baseURL = "/stat/overflow/api/v1"
axios.defaults.withCredentials = true
axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.headers.post['Content-Type'] = 'application/json'

axios.interceptors.response.use(res => {
    if (typeof res.data !== 'object') {
        return Promise.reject(res)
    }
    if (res.data.code != 200) {
        if (res.data.code == 416) {}
        if (res.data.data && window.location.hash == '#/login') {
            axios.defaults.headers['token'] = res.data.data
        }
        return Promise.reject(res.data)
    }
    return res.data
}, err => {
    // 此处返回的数据是axios.catch(err)中接收的数据
    return Promise.reject(err)
})

const http = axios
export default http
